﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(int id, string name, string pwd)
        {
            InitializeComponent();
            this.id = id;
            Name = name;
            this.pwd = pwd;
        }

        private int id;
        private string pwd;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = this.textBox1.Text;
            var pwd = this.textBox2.Text;
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var res = string.Format("update Load set Name='{0}',Password='{1}' where Id={2}",name,pwd,id);
                    var dt = DbHelper.AddOrDeleteOrEdittOrSave(res);
                    if (dt == 1)
                    {
                        MessageBox.Show("更新成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else 
                    {
                        MessageBox.Show("更新失败","提示");
                    }
                }
            }
            else 
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var res = string.Format("insert into Load values('{0}','{1}')",name,pwd);
                    var dt = DbHelper.AddOrDeleteOrEdittOrSave(res);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");
                    }
                }
            }
        }
    }
}
